#include "solver.h"

pwork *setup(params *p)
{
  idxint i = 0;
  double *ptr;
  static double b[60] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5, -0.5, 0.0, -0.5, -0.5, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0.0, -0.5, -0.5, 0.0, 0.0, 0.0, 1.0};
  b[13] = p->x_0;
  b[52] = p->one_over_q;
  static double c[64] = {1}; /* rest = {0.0}; */
  static double h[27]; /* = {0.0}; */
  static idxint q[5] = {3, 3, 3, 3, 3};
  static idxint Gjc[65] = {0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27};
  static idxint Gir[27] = {0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, 17, 6, 7, 8, 9, 18, 19, 20, 10, 21, 22, 23, 24, 25, 26, 11};
  static double Gpr[27] = {-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0};
  static idxint Ajc[65] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 16, 17, 18, 19, 20, 21, 22, 24, 27, 28, 29, 30, 31, 32, 33, 36, 39, 41, 43, 45, 47, 49, 51, 55, 57, 59, 61, 66, 68, 76, 84, 86, 88, 90, 92, 94, 97, 99, 101, 103, 105, 107, 109, 112, 115, 118, 120, 122, 125, 127, 129, 132};
  static idxint Air[132] = {26, 14, 16, 18, 20, 22, 24, 33, 34, 2, 12, 36, 37, 3, 14, 16, 39, 40, 45, 46, 50, 51, 4, 10, 50, 51, 52, 54, 55, 53, 57, 58, 59, 8, 57, 58, 0, 6, 42, 0, 1, 1, 2, 1, 3, 4, 5, 5, 6, 5, 7, 7, 18, 20, 48, 8, 9, 9, 10, 9, 11, 11, 22, 24, 54, 55, 12, 13, 14, 15, 18, 19, 22, 23, 47, 49, 16, 17, 20, 21, 24, 25, 41, 43, 26, 27, 26, 56, 27, 28, 27, 53, 28, 29, 28, 50, 51, 29, 30, 29, 44, 30, 31, 30, 38, 31, 32, 31, 35, 32, 33, 34, 35, 36, 37, 38, 39, 40, 39, 42, 40, 43, 44, 45, 46, 45, 48, 46, 49, 56, 57, 58};
  static double Apr[132] = {-1.0,1.0,1.0,1.0,1.0,1.0,1.0,-1.0,-1.0,0,1.0,-1.0,-1.0,0,-1.0,1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,1.0,0,0.5,0.5,1.0,-1.0,-1.0,0,-1.0,-1.0,1.0,1.0,0.5,0.5,1.0,0,1.0,-1.0,-1.0,1.0,-1.0,1.0,-1.0,-1.0,-1.0,1.0,-1.0,1.0,-1.0,0,-1.0,1.0,1.0,-1.0,-1.0,1.0,-1.0,1.0,-1.0,0,-1.0,1.0,0.5,-0.5,-1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,1.0,1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,-1.0,-1.0,1.0,-1.0,1.0,0.5,-0.5,1.0,-1.0,-1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,0,0.5,-0.5,0,0.5,-0.5,0,1.0,1.0,-1.0,-1.0,-1.0,-1.0,0,-1.0,-1.0,1.0,-1.0,1.0,-1.0,0,0.5,-0.5};
  static const idxint a_ind_map[3] = {9, 23, 37};
  for (i = 0; i < 3; ++i) {
    Apr[ a_ind_map[i] ] = p->a;
  }
  static const idxint b_ind_map[3] = {13, 51, 61};
  for (i = 0; i < 3; ++i) {
    Apr[ b_ind_map[i] ] = p->b;
  }
  Apr[129] = p->p;
  static const idxint q_ind_map[2] = {109, 115};
  for (i = 0; i < 2; ++i) {
    Apr[ q_ind_map[i] ] = p->q;
  }
  static const idxint r_ind_map[3] = {29, 112, 122};
  for (i = 0; i < 3; ++i) {
    Apr[ r_ind_map[i] ] = p->r;
  }
  return ECOS_setup(64 /* num vars */, 27 /* num cone constraints */, 60 /* num eq constraints */, 12 /* num linear cones */, 5 /* num second-order cones */, q, Gpr, Gjc, Gir, Apr, Ajc, Air, c, h ,b);
}

int solve(pwork *w, vars *sol)
{
  idxint i = 0;
  int exitflag = ECOS_solve(w);
  sol->x1 = w->x[9];
  sol->x2 = w->x[28];
  sol->x3 = w->x[19];
  sol->x4 = w->x[27];
  sol->x5 = w->x[12];
  sol->x6 = w->x[35];
  sol->x7 = w->x[39];
  return exitflag;
}

void cleanup(pwork *w)
{
  ECOS_cleanup(w,0);
}

